#include "task_scheduler.h"

Timer_t Timer1ms = {
    .bEnable = true,
    .hCounter = MIDDLE_FREQUENCY_TASK_PERIOD,
    .hCompare = MIDDLE_FREQUENCY_TASK_PERIOD,
    .bFlag = false,
};

Timer_t Timer10ms = {
    .bEnable = true,
    .hCounter = LOW_FREQUENCY_TASK_PERIOD,
    .hCompare = LOW_FREQUENCY_TASK_PERIOD,
    .bFlag = false,
};

Timer_t Timer500ms = {
    .bEnable = true,
    .hCounter = 1000,
    .hCompare = 1000,
    .bFlag = false,
};

Timer_t TimerGather = {
    .bEnable = false,
    .hCounter = 0,
    .hCompare = 0,
    .bFlag = false,
};

Timer_t TimerDiscrete = {
    .bEnable = false,
    .hCounter = 0,
    .hCompare = 0,
    .bFlag = false,
};

SystemTimer_t SystemTimer = {
    .oTimerNum = 5,
    .pTimer = {
        &Timer1ms,
        &Timer10ms,
        &Timer500ms,
        &TimerGather,
        &TimerDiscrete,
    }
};

void SystemTimerUpdate(void)
{
    u8 i = 0;
    for(i = 0;i < SystemTimer.oTimerNum; i++)
    {
        if(SystemTimer.pTimer[i]->bEnable)
        {
            SystemTimer.pTimer[i]->hCounter--;
            if(SystemTimer.pTimer[i]->hCounter == 0)
            {
                SystemTimer.pTimer[i]->bFlag = true;
            }
        }
    }
}

void TaskScheduler(void)
{
	extern uint8_t FanStartCnt;
    if(Timer1ms.bFlag)   //20ms
    {
        Timer1ms.bFlag = false;
        Timer1ms.hCounter = Timer1ms.hCompare;
//        (void)MC_GetVeloRef();
        Fault_LED_Disp();
        MiddleFreqTask();
        FiniteStateMachine();
			
        #if defined(BEEP_SURPPORT)
        if(Beep.status == 1)
        {
            if(Beep.cnt-- == 0)
            {
                Beep.cnt = BEEP_ON_TIME;
                Beep.status = 0;
 //               BEEP_OFF;
            }
        }
        #endif
				if(FanStartCnt>0)
				{
					FanStartCnt--;
					if(FanStartCnt == 0)
					{
							#if(CONTROL_MODE==0)
									SignalChain.hVSPointer = LEVEL6_SPEED_CMD;
							#else
									SignalChain.hPowref = LEVEL6_POWER_CMD;
							#endif
							ZiRanFan=0;
							Beep.status = 1;
							
						 FAN_Cmd = CMD_START;
												Command = FAN_Cmd;
					}
				}
					
				
    }
    
    if(Timer10ms.bFlag)  //1ms;
    {
        Timer10ms.bFlag = false;
        Timer10ms.hCounter = Timer10ms.hCompare;
        LowFreqTask();		
    }
    
    if(Timer500ms.bFlag)
    {
        Timer500ms.bFlag = false;
        Timer500ms.hCounter = Timer500ms.hCompare;
        if(OnOffTime.status == 1)
        {
            if(OnOffTime.timeCnt < OnOffTime.timeEnd)
                OnOffTime.timeCnt++;
            else
            {
                OnOffTime.status = 0;
                Command = CMD_STOP;
                RELAY_OFF;
                #if defined(BEEP_SURPPORT)
                Beep.status = 1;
                Beep.cnt = BEEP_ON_TIME;
                #endif

                FAN_Shake   = 0;
                ZiRanFan    = 0;
            }
        }
				
        //---------------------------------
        if(ZiRanFan==1)
        {
            if(++ZiRanCnt>=20)
            {
                ZiRanCnt=0;
                if(FlagZIRan==0)
                {
                    ZiRanMode++;
                    if(ZiRanMode>=5)FlagZIRan=1;
                }
                else if(FlagZIRan==1)
                {
                    ZiRanMode--;
                    if(ZiRanMode==0)FlagZIRan=0;
                }
            }
            #if(CONTROL_MODE == 1)
            if(ZiRanMode==0)     SignalChain.hPowref = LEVEL1_POWER_CMD;
            else if(ZiRanMode==1)SignalChain.hPowref = LEVEL3_POWER_CMD;
            else if(ZiRanMode==2)SignalChain.hPowref = LEVEL5_POWER_CMD;
            else if(ZiRanMode==3)SignalChain.hPowref = LEVEL2_POWER_CMD;
            else if(ZiRanMode==4)SignalChain.hPowref = LEVEL4_POWER_CMD;
            else if(ZiRanMode==5)SignalChain.hPowref = LEVEL6_POWER_CMD;
            #elif (CONTROL_MODE == 0)
            if(ZiRanMode==0)     SignalChain.hVSPointer = LEVEL1_SPEED_CMD;
            else if(ZiRanMode==1)SignalChain.hVSPointer = LEVEL3_SPEED_CMD;
            else if(ZiRanMode==2)SignalChain.hVSPointer = LEVEL5_SPEED_CMD;
            else if(ZiRanMode==3)SignalChain.hVSPointer = LEVEL2_SPEED_CMD;
            else if(ZiRanMode==4)SignalChain.hVSPointer = LEVEL4_SPEED_CMD;
            else if(ZiRanMode==5)SignalChain.hVSPointer = LEVEL6_SPEED_CMD;
            #endif
                
        }
        //----------------------
        if(FlagSwitchLed==1)
        {
            if(++SwitchLEDCnt>=2)
            {
                SwitchLEDCnt=0;
                FlagSwitchLed=0;
                FAN_Shake=1;RELAY_ON;
            }
        }
				//------------------------------------
				if(FlashCnt<20)
				{
					FlashCnt++;
				}
				else
				{
					FlashSet = 1;
				}
    }
}


